<?php

namespace App\Http\Controllers;

use App\WxUser;
use Illuminate\Http\Request;

class WxUserController extends Controller {

	protected $model;

	public function __construct(WxUser $user) {
		$this->model = $user;
	}

	public function store(Request $request) {
		$this->validate($request, [
			'open_id' => 'required',
			'nick_name' => 'required',
			'image' => 'required',
		]);
		$user = $this->model->firstOrCreate(['open_id' => $request->open_id]);
		$user->name = $request->nick_name;
		$user->avatar = $request->image;
		$user->save();
		return json_encode(['code' => 200, 'msg' => 'success', 'data' => $user]);
	}
}
